cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_STANDARD 11)

if(${ANDROID_ABI} EQUAL armeabi-v7a)
        set(arch arm7)
endif()
if(${ANDROID_ABI} EQUAL arm64-v8a)
        set(arch aarch64)
endif()
if(${ANDROID_ABI} EQUAL x86)
        set(arch i386)
endif()
if(${ANDROID_ABI} EQUAL x86_64)
        set(arch x86_64)
endif()

# dirs
set(MAIN_NAME           kamailio                )

set(cfg_dir             etc/${MAIN_NAME}/       )
set(share_dir           share/${MAIN_NAME}/     )
set(run_dir             var/run/${MAIN_NAME}/   )

set(prefix              /usr/local              )

set(cfg_target          ${prefix}/${cfg_dir}    )
set(share_target        ${prefix}/${share_dir}  )
set(run_target          ${prefix}/${run_dir}    )

add_definitions(
        -DNAME="${MAIN_NAME}"
        -DVERSION="5"
        -DARCH="${arch}"
        -DOS="linux_"
        -DOS_QUOTED="linux"
        -DCOMPILER="gcc 4.9.2"
        -DVERSIONVAL=5005001
        -DCFG_DIR="${cfg_target}"
        -DSHARE_DIR="${share_target}"
        -DRUN_DIR="${run_target}"
)

add_definitions(
        -DUSE_FUTEX
        -DHAVE_GETHOSTBYNAME2
        -DHAVE_UNION_SEMUN
        -DHAVE_SCHED_YIELD
        -DHAVE_MSG_NOSIGNAL
        -DHAVE_MSGHDR_MSG_CONTROL
        -DHAVE_ALLOCA_H
        -DHAVE_TIMEGM
        -DHAVE_SCHED_SETSCHEDULER
        -DHAVE_IP_MREQN
)

set(modules_search_path lib/${ANDROID_ABI})

add_definitions(
        -DSHM_MMAP
        -DUSE_SIGWAIT
        -DMODS_DIR="${modules_search_path}"
        -DNOSMP
        -DUSE_TCP
        -DHAVE_GETHOSTBYNAME2
        -DUSE_PTHREAD_MUTEX
        -DHAVE_UNION_SEMUN
        -DHAVE_SCHED_YIELD
        -DQ_MALLOC
        -DTLSF_MALLOC
        -DF_MALLOC
        -DMALLOC_STATS
        -DUSE_SCTP
        -DUSE_DST_BLOCKLIST
        -DWITH_EVENT_LOCAL_REQUEST
        -DSTATISTICS
)

include_directories(core/)
include_directories(lib/srdb1/)
include_directories(sqlite3/)

file(GLOB_RECURSE CORE_SRC core/*.c)
file(GLOB_RECURSE SRDB1_SRC lib/srdb1/*.c)
file(GLOB_RECURSE DB_SQLITE_SRC modules/db_sqlite/*.c)
file(GLOB_RECURSE SQLITE3_SRC sqlite3/*.c)
file(GLOB_RECURSE AUTH_SRC modules/auth/*.c)
file(GLOB_RECURSE AUTH_DB_SRC modules/auth_db/*.c)
file(GLOB_RECURSE JSONRPCS_SRC modules/jsonrpcs/*.c)
file(GLOB_RECURSE TM_SRC modules/tm/*.c)
file(GLOB_RECURSE KEX_SRC modules/kex/*.c)
file(GLOB_RECURSE COREX_SRC modules/corex/*.c)
file(GLOB_RECURSE TMX_SRC modules/tmx/*.c)
file(GLOB_RECURSE SL_SRC modules/sl/*.c)
file(GLOB_RECURSE RR_SRC modules/rr/*.c)
file(GLOB_RECURSE PV_SRC modules/pv/*.c)
file(GLOB_RECURSE MAXFWD_SRC modules/maxfwd/*.c)
file(GLOB_RECURSE USRLOC_SRC modules/usrloc/*.c)
file(GLOB_RECURSE REGISTRAR_SRC modules/registrar/*.c)
file(GLOB_RECURSE TEXTOPS_SRC modules/textops/*.c)
file(GLOB_RECURSE TEXTOPSX_SRC modules/textopsx/*.c)
file(GLOB_RECURSE SIPUTILS_SRC modules/siputils/*.c)
file(GLOB_RECURSE XLOG_SRC modules/xlog/*.c)
file(GLOB_RECURSE SANITY_SRC modules/sanity/*.c)
file(GLOB_RECURSE CTL_SRC modules/ctl/*.c)
file(GLOB_RECURSE CFG_RPC_SRC modules/cfg_rpc/*.c)
file(GLOB_RECURSE ACC_SRC modules/acc/*.c)
file(GLOB_RECURSE COUNTERS_SRC modules/counters/*.c)

add_library( srdb1 STATIC ${SRDB1_SRC})
add_library( auth STATIC ${AUTH_SRC})
add_library( sqlite3 STATIC ${SQLITE3_SRC})
add_library( ifaddrs STATIC ifaddrs/ifaddrs.c)

add_library( kamailio SHARED ${CORE_SRC} kamailio.cpp main.c)
add_library( db_sqlite SHARED ${DB_SQLITE_SRC})
add_library( auth_db SHARED ${AUTH_DB_SRC})
add_library( tm SHARED ${TM_SRC})
add_library( jsonrpcs SHARED ${JSONRPCS_SRC})
add_library( kex SHARED ${KEX_SRC})
add_library( corex SHARED ${COREX_SRC})
add_library( tmx SHARED ${TMX_SRC})
add_library( sl SHARED ${SL_SRC})
add_library( rr SHARED ${RR_SRC})
add_library( pv SHARED ${PV_SRC})
add_library( maxfwd SHARED ${MAXFWD_SRC})
add_library( usrloc SHARED ${USRLOC_SRC})
add_library( registrar SHARED ${REGISTRAR_SRC})
add_library( textops SHARED ${TEXTOPS_SRC})
add_library( textopsx SHARED ${TEXTOPSX_SRC})
add_library( siputils SHARED ${SIPUTILS_SRC})
add_library( xlog SHARED ${XLOG_SRC})
add_library( sanity SHARED ${SANITY_SRC})
add_library( ctl SHARED ${CTL_SRC})
add_library( cfg_rpc SHARED ${CFG_RPC_SRC})
add_library( acc SHARED ${ACC_SRC})
add_library( counters SHARED ${COUNTERS_SRC})

target_link_libraries(kamailio log ifaddrs)
target_link_libraries(db_sqlite kamailio srdb1 sqlite3 log)
target_link_libraries(auth_db kamailio srdb1 auth log)
target_link_libraries(tm kamailio log)
target_link_libraries(jsonrpcs kamailio tm log)
target_link_libraries(kex kamailio log)
target_link_libraries(corex kamailio log)
target_link_libraries(tmx kamailio log)
target_link_libraries(sl kamailio log)
target_link_libraries(rr kamailio log)
target_link_libraries(pv kamailio log)
target_link_libraries(maxfwd kamailio log)
target_link_libraries(usrloc kamailio srdb1 log)
target_link_libraries(registrar kamailio log)
target_link_libraries(textops kamailio log)
target_link_libraries(textopsx kamailio log)
target_link_libraries(siputils kamailio log)
target_link_libraries(xlog kamailio log)
target_link_libraries(sanity kamailio log)
target_link_libraries(ctl kamailio log)
target_link_libraries(cfg_rpc kamailio log)
target_link_libraries(acc kamailio srdb1 log)
target_link_libraries(counters kamailio log)
